using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;

namespace Ramparts
{
    class MainMenu : Scene
    {
        Rectangle viewportRect;
        SpriteFont gameFont;
        int selectedItem = 0;
        double time;
        KeyboardState oldState;
        

        List<MenuItem> menuItems = new List<MenuItem>();
        
        public MainMenu(SceneManager sceneMgr)
        {
            sceneManager = sceneMgr;
        }

        public override bool initScene()
        {

            base.initScene();
            backgroundTexture = sceneManager.rampart.content.Load<Texture2D>("Content\\title_bg");
            // GameComponent image = new GameComponent("Content\\SquareGuy", new Vector2(200,200) , sceneManager);
    //      GameComponent image2 = new GameComponent("Content\\SquareGuy", new Vector2(100, 100), sceneManager);
            gameFont = sceneManager.rampart.content.Load<SpriteFont>("Content\\Fonts\\MainGameFont");

   //         addComponentToScene(image);
   //         addComponentToScene(image2);

            //This is for adding my menu items
            MenuItem newGame = new MenuItem("New Game", new Vector2(320.0f, 250.0f));
            MenuItem options = new MenuItem("Options", new Vector2(320.0f, 300f));
            MenuItem exit = new MenuItem("Exit", new Vector2(320f, 350f));

            menuItems.Add(newGame);
            menuItems.Add(options);
            menuItems.Add(exit);

            //Create a Rectangle that represents the full
            //drawable area of the game screen.
            viewportRect = new Rectangle(0, 0,
                sceneManager.rampart.graphics.GraphicsDevice.Viewport.Width,
                sceneManager.rampart.graphics.GraphicsDevice.Viewport.Height);
            oldState = Keyboard.GetState();

            // Set the camera for the main menu
            camera.ManualCamera = false;
            camera.Position = new Vector2(-1*viewportRect.Width/2, -1*viewportRect.Height/2);


            return true;
        }

        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
          
            time = gameTime.TotalGameTime.TotalSeconds;

            KeyboardState currentState = Keyboard.GetState();

            if (currentState.IsKeyDown(Keys.Down))
            {
                if (!oldState.IsKeyDown(Keys.Down))
                {
                    selectedItem++;
                }
            }

            if (currentState.IsKeyDown(Keys.Up))
            {
                if (!oldState.IsKeyDown(Keys.Up))
                {
                    selectedItem--;
                }
            }
                    

            // roll around to the top or bottom if we get that far
            if (selectedItem == 3)
            {
                selectedItem = 0;
            }
            if (selectedItem == -1)
            {
                selectedItem = 2;
            }

            if (currentState.IsKeyDown(Keys.Enter))
            {
                if (!oldState.IsKeyDown(Keys.Enter))
                {
                    switch (selectedItem)
                    {
                        case 0:
                            // new game stuff
                            BuildPhase build = new BuildPhase(sceneManager);
                            build.initScene();
                            sceneManager.AddScene(build);
                            break;
                        case 1:
                            // options screen
                            OptionsMenu options = new OptionsMenu(sceneManager);
                            options.initScene();
                            sceneManager.AddScene(options);
                            break;
                        case 2:
                            // exit
                            sceneManager.rampart.Exit();
                            break;
                    }
                }
            }

            oldState = currentState;

        }

        public override void drawScene(){


            base.drawScene();

            sceneManager.rampart.spriteBatch.Draw(backgroundTexture, viewportRect, Color.White);

            for (int i = 0; i < 3; i++)
            {
                Color itemColor = Color.White;
                float scale = 1;
                if (i == selectedItem)
                {
                    itemColor = Color.Yellow;
                    float pulsate = (float)Math.Sin(time * 6) + 1;
                    scale = 1 + 0.1f * pulsate;

                }
                sceneManager.rampart.spriteBatch.DrawString(gameFont, menuItems[i].GetText(),
                                                            menuItems[i].GetPosition(), itemColor, 0f, 
                                                            new Vector2(0,gameFont.LineSpacing /2),scale,SpriteEffects.None,0);
            }

            sceneManager.rampart.spriteBatch.End();
        }
    }
}
